package com.tax.domain.service.perform.submit;

import com.alibaba.fastjson.JSONObject;
import com.tax.config.exception.BusinessException;
import com.tax.domain.constant.businessconstant.BusinessConstant;
import com.tax.domain.dto.machine.PerformSubmitContext;
import com.tax.domain.service.perform.PerformSubmitI;
import com.tax.infrastructure.extension.Extension;
import com.tax.infrastructure.util.CompletableFutureUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@RequiredArgsConstructor
@Extension(useCase = BusinessConstant.UseCase.PERFORM_CREATE,scenario = BusinessConstant.SubmitScenario.SERVICE_ORDER)
public class SingleServiceOrderSubmitService implements PerformSubmitI {

    @Resource
    protected ThreadPoolExecutor performTaskExecutor;

    @Autowired
    protected TransactionTemplate transactionTemplate;
    @Override
    public void execute(PerformSubmitContext submitContext) {
        paramValid(submitContext);
        prepareData(submitContext);
        dataPersistence(submitContext);
        singleServiceOrderSync(submitContext);
    }

    private void singleServiceOrderSync(PerformSubmitContext submitContext) {
        log.info("singleServiceOrderSync:{}",submitContext);
        try{
            performTaskExecutor.execute(()->{
                //执行一些异步操作，比如给客户发短信、发邮件之类的
            });
        }catch (Exception e){

        }
    }

    private Integer dataPersistence(PerformSubmitContext submitContext) {
        try{
            return transactionTemplate.execute(e ->{
                //数据入库操作
                return 1;
            });
        }catch (Exception e){
            throw new BusinessException("订单数据保存失败");
        }
    }

    private void prepareData(PerformSubmitContext submitContext) {
        List<CompletableFuture<Void>> tasks = commonPrepareData(submitContext);
        prepareDataHandler(tasks,submitContext);
    }

    /**
     * 准备数据任务处理
     * @param tasks
     * @param context
     */
    protected void prepareDataHandler(List<CompletableFuture<Void>> tasks,PerformSubmitContext context){
        CompletableFutureUtil.blockWaitingUntilFinished(tasks, JSONObject.toJSONString(context.getReq()));
    }

    protected List<CompletableFuture<Void>> commonPrepareData(PerformSubmitContext context){
        List<CompletableFuture<Void>> tasks = new ArrayList<>(
                Arrays.asList(
                        CompletableFuture.runAsync(()->setServiceNo(context),performTaskExecutor),
                        CompletableFuture.runAsync(()->setOrderSkuData(context),performTaskExecutor)
                )
        );
        return tasks;
    }

    private void setOrderSkuData(PerformSubmitContext context) {

    }

    private void setServiceNo(PerformSubmitContext context) {

    }

    private void paramValid(PerformSubmitContext submitContext) {

    }
}
